#define  _CRT_SECURE_NO_WARNINGS

class Solution {
public:
    int longestWPI(vector<int>& hours) {
        int n = hours.size();
        unordered_map<int, int> perfix_map;
        int perfix_sum = 0;
        int max_length = 0;
        for (int i = 0; i < n; i++)
        {
            perfix_sum += hours[i] > 8 ? 1 : -1;

            if (perfix_sum > 0)
                max_length = i + 1;
            else if (perfix_map.count(perfix_sum - 1))
                max_length = max(max_length, i - perfix_map[perfix_sum - 1]);
            if (perfix_map.count(perfix_sum) == false)
                perfix_map[perfix_sum] = i;
        }
        return max_length;
    }
};